package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.entity.Address;
import com.hoongs.v1.service.AddressService;
import com.hoongs.v1.utils.Result;
import com.hoongs.v1.vo.AddressVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/address")
@Validated
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/selectInfoByUser")
    public Result selectInfoByUser(String id, @RequestHeader("token") String token) {
        return addressService.selectInfoByUser(id, token);
    }

    @GetMapping("/selectDefaultByUser")
    public Result selectDefaultByUser (@RequestHeader("token") String token) {
        return addressService.selectDefaultByUser(token);
    }

    @GetMapping("/selectListByUser")
    public Result selectListByUser(@RequestHeader("token") String token) {
        return addressService.selectListByUser(token);
    }

    @PostMapping("/saveByUser")
    public Result saveByUser(AddressVo addressVO, @RequestHeader("token") String token) {
        return addressService.saveByUser(addressVO, token);
    }

    @PostMapping("/removeByUser")
    public Result removeByUser(String id, @RequestHeader("token") String token) {
        return addressService.removeByUser(id, token);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Address address, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : addressService.add(address);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return addressService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Address address) {
        return addressService.update(address);
    }


    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(Integer page, Integer size, String userId, String userName) {
        return addressService.selectPageList(page, size, userId, userName);
    }
}
